<template>
  <div class="portarea-detail">
    <el-page-header @back="goBack" title="返回区域管理">
      <template #content>
        <span class="page-title">区域详情</span>
      </template>
    </el-page-header>
    
    <el-card class="detail-card" v-loading="loading">
      <template #header>
        <div class="card-header">
          <span>基本信息</span>
        </div>
      </template>
      
      <el-descriptions :column="2" border>
        <el-descriptions-item label="区域ID">{{ area.areaId }}</el-descriptions-item>
        <el-descriptions-item label="区域编号">{{ area.areaCode }}</el-descriptions-item>
        <el-descriptions-item label="区域名称">{{ area.areaName }}</el-descriptions-item>
        <el-descriptions-item label="区域类型">{{ area.areaType }}</el-descriptions-item>
        <el-descriptions-item label="最大容量">{{ area.maxCapacity }}</el-descriptions-item>
        <el-descriptions-item label="当前使用">{{ area.currentUsage }}</el-descriptions-item>
        <el-descriptions-item label="使用率">{{ calculateUsage(area) }}%</el-descriptions-item>
        <el-descriptions-item label="状态">
          <el-tag :type="getStatusType(area.status)">{{ area.status || '未知' }}</el-tag>
        </el-descriptions-item>
      </el-descriptions>
    </el-card>
  </div>
</template>

<script>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import axios from 'axios'

export default {
  name: 'PortAreaDetail',
  setup() {
    const route = useRoute()
    const router = useRouter()
    const area = ref({})
    const loading = ref(false)
    
    const fetchAreaDetail = async () => {
      const areaId = route.params.id
      if (!areaId) return
      
      loading.value = true
      try {
        const response = await axios.get(`/port-areas/${areaId}`)
        area.value = response.data.data
        console.log('获取的区域详情:', area.value)
      } catch (error) {
        console.error('获取区域详情失败:', error)
        ElMessage.error('获取区域详情失败')
      } finally {
        loading.value = false
      }
    }
    
    const calculateUsage = (area) => {
      if (!area.maxCapacity || area.maxCapacity === 0) return 0
      return Math.round((area.currentUsage / area.maxCapacity) * 100)
    }
    
    const getStatusType = (status) => {
      if (!status) return 'info'
      
      switch (status) {
        case '可用':
          return 'success'
        case '维护':
          return 'warning'
        case '已满':
          return 'danger'
        default:
          return 'info'
      }
    }
    
    const goBack = () => {
      router.back()
    }
    
    onMounted(() => {
      fetchAreaDetail()
    })
    
    return {
      area,
      loading,
      goBack,
      calculateUsage,
      getStatusType
    }
  }
}
</script>

<style scoped>
.portarea-detail {
  padding: 20px;
}

.detail-card {
  margin-top: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.page-title {
  font-weight: bold;
  font-size: 18px;
}
</style>